// pages/car/startcar/index.ts
import {save,SecretRealm,uploadImage} from '../../../api/secret_realm'
import { LENGYU_SUCCESS_CODE } from '../../../utils/request';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    progress: 0,
  
    error:"",
    formData: {
      orderType:"",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  selectFile(e:any){
    console.log("选择文件",e)
    let that= this
    wx.chooseMessageFile({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        console.log(res.tempFiles[0].path)
        console.log(res.tempFiles[0].size)
        that.uploadFile(res.tempFiles[0].path,res.tempFiles[0].size);
      }
    })
  },

  uploadFile(url:string,size:number){
    const intervalId = setInterval(() => {
      let progress =  wx.getStorageSync("upload_progress")
      console.log('每隔 2 秒读取一次上传记录' + progress);
     
      this.setData({
        progress:progress*100
      })
      if(progress ==1){
        clearInterval(intervalId);
      }
  }, 2000);

    uploadImage(url,{},size).then((result) => {
        console.log(result); // 输出: 返回的字符串
        clearInterval(intervalId);
    });
    
  },
 async submitForm(){
   console.log("校验信息：" ,this.data.formData)
    this.selectComponent('#form').validate( async (valid:any, errors:any) =>{
      console.log('valid', valid, errors)
      if (!valid) {
          const firstError = Object.keys(errors)
          if (firstError.length) {
              this.setData({
                  error: errors[firstError[0]].message
              })
              wx.showToast({
                title: this.data.error,
                icon: 'error',
                duration: 2000
              })
          }
      } else {
        
      }
    })
  }
})